﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Radar;
using Jage.Editor.Logic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Jage.Editor
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            Map = null;
            Filename = null;
            Saved = false;
        }

        private MapDefinition Map
        {
            get
            {
                return mapRenderControl1.Map;
            }
            set
            {
                if (value != null)
                {
                    mapRenderControl1.Map = value;
                    UpdateIOVisualization();
                    UpdateScrollBars();
                    toolStrip1.Visible = true;
                }
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
        }

        #region I/O (File menu)

        private bool _saved;
        private bool Saved
        {
            get
            {
                return _saved;
            }
            set
            {
                _saved = value;
                UpdateIOVisualization();
            }
        }

        private string _filename;
        private string Filename
        {
            get { return _filename; }
            set { _filename = value; UpdateIOVisualization(); }
        }

        private void UpdateIOVisualization()
        {
            UpdateFormTitle();
            UpdateFileMenuItems();
        }

        private void UpdateFileMenuItems()
        {
            saveToolStripMenuItem.Enabled = Map != null;
            saveAsToolStripMenuItem.Enabled = Map != null;
        }

        private void UpdateFormTitle()
        {
            string filename;
            if (Filename == null)
            {
                filename = "<unsaved map>";
            }
            else
            {
                filename = Filename;
            }
            string asterisk = string.Empty;
            if (!Saved) asterisk = " *";

            this.Text = "Jage map editor - " + filename + asterisk;
        }

        private void neToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (SaveMapIfNeeded())
            {
                NewMapPropertiesForm mapPropertiesForm = new NewMapPropertiesForm();
                if (mapPropertiesForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Map = mapPropertiesForm.MapDefinition;
                    Filename = null;
                    Saved = false;
                }
            }
        }

        private bool SaveMapIfNeeded()
        {
            if (Map != null && !Saved)
            {
                var result = MessageBox.Show("Your current map is not saved.  Save first?", "Map unsaved", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (result == System.Windows.Forms.DialogResult.Cancel) return false;
                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    return SaveMap(false);
                }
                return true;
            }
            else
            {
                return true;
            }
        }

        private bool SaveMap(bool saveAs)
        {
            if (Map != null)
            {
                if (Filename == null || saveAs)
                {
                    if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        Filename = saveFileDialog1.FileName;
                    }
                    else
                    {
                        return false;
                    }
                }
                MapDefinition.SaveToFile(Filename, Map);
                Saved = true;
            }
            return true;
        }

        private void LoadMap()
        {
            if (SaveMapIfNeeded())
            {
                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Filename = openFileDialog1.FileName;
                    Map = MapDefinition.LoadFromFile(Filename);
                    Saved = true;
                }
            }
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadMap();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveMap(false);
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            SaveMap(true);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (SaveMapIfNeeded())
            {
                this.Close();
            }
        }

        #endregion

        #region Placing items

        private void plainToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mapRenderControl1.DrawItemMode = DrawItemMode.Terrain;
        }

        private void tree1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mapRenderControl1.DrawItemMode = DrawItemMode.Foliage;
        }
        
        #endregion

        #region Scroll map

        private void UpdateScrollBars()
        {
            if (Map != null)
            {
                // divide px sizes by 10 else the scrollhandlers are small.

                int mapWidthPx = Map.Width * 90;
                int mapHeightPx = Map.Height * 60;

                vScrollBar1.Enabled = true;
                hScrollBar1.Enabled = true;
                vScrollBar1.SmallChange = mapHeightPx / 100;
                vScrollBar1.LargeChange = mapHeightPx / 20;
                vScrollBar1.Minimum = 0;
                vScrollBar1.Maximum = (mapHeightPx - (int)(mapRenderControl1.Height));

                hScrollBar1.SmallChange = mapWidthPx / 100;
                hScrollBar1.LargeChange = mapWidthPx / 20;
                hScrollBar1.Minimum = 0;
                hScrollBar1.Maximum = (mapWidthPx - (int)(mapRenderControl1.Width));
            }
            else
            {
                vScrollBar1.Enabled = false;
                hScrollBar1.Enabled = false;
            }
        }

        private void MainForm_ResizeEnd(object sender, EventArgs e)
        {
            UpdateScrollBars();
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            mapRenderControl1.Offset = new Point(e.NewValue, vScrollBar1.Value);
        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            mapRenderControl1.Offset = new Point(hScrollBar1.Value, e.NewValue);
        }

        private void mapRenderControl1_TerrainDragged(object sender, EventArgs e)
        {
            hScrollBar1.Value = mapRenderControl1.Offset.X;
            vScrollBar1.Value = mapRenderControl1.Offset.Y;
        }

        #endregion

       
    }
}
